﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SchoolsBI;

public partial class Cursos_Cursos : System.Web.UI.Page
{
    static Colegio Col= new Colegio();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) { FillGrid(); }
    }
    protected void cmdAccion_Click(object sender, EventArgs e)
    {
        Save();
        FillGrid();
    }

    private void FillGrid()
    {
        GridView1.DataSource = Colegio.Tabla();
        GridView1.DataKeyNames = new string[] { "IdColegio" };
        GridView1.AutoGenerateColumns = false;
        GridView1.DataBind();
        cmdAccion.Text = "Nuevo";
        txtNombre.Text = "";
        

    }

    void Save(){
        switch (cmdAccion.Text) { 
            case "Nuevo":
                Col.DescripcionColegio= txtNombre.Text;
                Col.Crear();
                break;
            case "Modificar":
                Col.DescripcionColegio = txtNombre.Text;
                Col.Actualizar();
                break;
            case "Eliminar":
                Col.Borrar();
                break;
        }
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

        Col = Colegio.Buscar("idColegio=" + GridView1.SelectedValue.ToString());
        showData();
        cmdAccion.Text = "Modificar";
    }
    
    void showData(){
        txtNombre.Text = Col.DescripcionColegio;
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Col = Colegio.Buscar("idColegio=" + GridView1.DataKeys[e.RowIndex].Value);
        showData();
        cmdAccion.Text = "Eliminar";
    }
}
